package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.CzBasHeatsource;
import com.seawaterbt.ssm.entity.ShengBasHeatsource;
import com.seawaterbt.ssm.mapper.ShengBasHeatsourceMapper;
import com.seawaterbt.ssm.service.CzBasHeatsourceService;
import com.seawaterbt.ssm.service.ShengBasHeatsourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ShengBasHeatsourceServiceImpl extends ServiceImpl<ShengBasHeatsourceMapper, ShengBasHeatsource> implements ShengBasHeatsourceService {

    @Autowired private CzBasHeatsourceService czBasHeatsourceService;
    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean saveOrUpdate(ShengBasHeatsource entity) {
        return super.saveOrUpdate(entity);
    }

    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean save(ShengBasHeatsource entity) {
        return super.save(entity);
    }


    @Override
    public void toSeng() {

        QueryWrapper<CzBasHeatsource> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("code",130981011).or().likeRight("code",130903007);
        List<CzBasHeatsource> czlist = czBasHeatsourceService.list(queryWrapper);
        if (czlist != null){
            for (CzBasHeatsource czBasHeatsource:czlist){
                ShengBasHeatsource shengBasHeatsource = new ShengBasHeatsource();
                shengBasHeatsource.setADDTIME(czBasHeatsource.getCreateTime());
                shengBasHeatsource.setAREAPUBLIC(Double.parseDouble(czBasHeatsource.getAreaPublic()==null?"0.0":czBasHeatsource.getAreaPublic()));
                shengBasHeatsource.setAREARESIDENT(Double.parseDouble(czBasHeatsource.getAreaResident()==null?"0.0":czBasHeatsource.getAreaResident()));
                shengBasHeatsource.setAREARSELF(Double.parseDouble(czBasHeatsource.getAreaRself()==null?"0.0":czBasHeatsource.getAreaRself()));
                shengBasHeatsource.setBUILDNATURE(czBasHeatsource.getBuildNature().equals("委托")?1:0);
                int energytype = 0;
                if (!StringUtils.isEmpty(czBasHeatsource.getEnergyType())){
                    if ("天然气".equals(czBasHeatsource.getEnergyType())){
                        energytype=0;
                    }else if ("煤".equals(czBasHeatsource.getEnergyType())){
                        energytype=1;
                    }else if ("热泵".equals(czBasHeatsource.getEnergyType())){
                        energytype=2;
                    }else if ("热电联产".equals(czBasHeatsource.getEnergyType())){
                        energytype=3;
                    }else if ("工业余热".equals(czBasHeatsource.getEnergyType())){
                        energytype=4;
                    }else if ("电锅炉".equals(czBasHeatsource.getEnergyType())){
                        energytype=5;
                    }else if ("其他".equals(czBasHeatsource.getEnergyType())){
                        energytype=6;
                    }
                    shengBasHeatsource.setENERGYTYPE(energytype);
                }
                shengBasHeatsource.setCITYID(czBasHeatsource.getRegionCode());
                shengBasHeatsource.setHEATCOMPANYID(czBasHeatsource.getCode().startsWith("130981011")?"130981011":"130903007");
                shengBasHeatsource.setHEATINGAREA(Double.parseDouble(czBasHeatsource.getHeatingArea()==null?"0.0":czBasHeatsource.getHeatingArea()));
                shengBasHeatsource.setHEATINGCAPACITYRATED(Double.parseDouble(czBasHeatsource.getHeatingCapacityRated()==null?"0.0":czBasHeatsource.getHeatingCapacityRated()));
                shengBasHeatsource.setHEATSOURCEADDRESS(czBasHeatsource.getAddress());
                shengBasHeatsource.setHEATSOURCEID(czBasHeatsource.getCodeProvince());
                shengBasHeatsource.setHEATSOURCENAME(czBasHeatsource.getHeatSourceName());
                shengBasHeatsource.setMAPX(Double.parseDouble(czBasHeatsource.getMapx()==null?"0":czBasHeatsource.getMapx()));
                shengBasHeatsource.setMAPY(Double.parseDouble(czBasHeatsource.getMapy()==null?"0":czBasHeatsource.getMapy()));
                shengBasHeatsource.setNOTE(czBasHeatsource.getNote());
                shengBasHeatsource.setREGIONAL("130");
                shengBasHeatsource.setTELDUTY(czBasHeatsource.getDutyCalls());
                this.save(shengBasHeatsource);
            }
        }
    }
}
